$(function() {
    //获取关键字
    var keyWord = localStorage.search;
    $(".search").attr("placeholder",keyWord);
    //历史回退
    $(".cancelBtn").on("click",function(){
        history.back();
    });
    //历史搜索
    if(localStorage.keyWordList) {
        var tempArr = localStorage.keyWordList.split(",");
        var wordListArr = [];
        for(var i=0;i<tempArr.length;i++) {
            var flag = false;
            for(var j=0;j<wordListArr.length;j++) {
                if(tempArr[i]===wordListArr[j]) {
                    flag = true;
                    break;
                }
            }
            if(!flag) {
                wordListArr.push(tempArr[i]);
            }
        }
        $(".historyKeyWord").html(template("historyKeyWord",{wordListArr:wordListArr}));
    }
    //热门搜索
    $.ajax({
        type: "get",
        // url: domain.test + "/getkeyword",
        url: domain.online + "/api/index/keyword",
        dataType: "json",
        success: function (response) {
            // if(response.data.rtnCode===200) {
            if(response.rtnCode===200) {
                var hotKeyWord = response.data.data;
                var hotKeyWord = response.data;
                var hotKeyWordArr = hotKeyWord.split(";");
                $(".hotKeyWord").html(template("hotKeyWord",{hotKeyWordList:hotKeyWordArr}));
            }
        }
    });
    //添加历史搜索
    $(".hotKeyWord").on("click",".keyWordBtn",function() {
        var searchKeyWord = $(this).html();
        if(localStorage.keyWordList) {
            localStorage.keyWordList = localStorage.keyWordList + "," + searchKeyWord; 
        }else {
            localStorage.keyWordList = searchKeyWord;
        }
        location.href = "./searchResult.html?keyword=" + searchKeyWord;
    });
    $(".historyKeyWord").on("click",".keyWordBtn",function() {
        var searchKeyWord = $(this).html();
        location.href = "./searchResult.html?keyword=" + searchKeyWord;
    })
    //清空历史搜索
    $(".delIcon").on("click",function() {
        if(localStorage.keyWordList) {
            localStorage.clear();
            location.reload();
        }
    })
    //搜索框搜索
    $(".search").on("search",function() {
        var word = $(this).val();
        if(!word) {
            word = $(this).attr("placeholder");
            if(word) {
                if(localStorage.keyWordList) {
                    localStorage.keyWordList = localStorage.keyWordList + "," + word; 
                }else {
                    localStorage.keyWordList = word;
                }
                location.href = "./searchResult.html?keyword=" + word;
            }else {
                alert("请输入你要搜索的宝贝!");
            }
        }else {
            if(localStorage.keyWordList) {
                localStorage.keyWordList = localStorage.keyWordList + "," + word; 
            }else {
                localStorage.keyWordList = word;
            }
            location.href = "./searchResult.html?keyword=" + word;
        }
    });
});